﻿Imports System.Windows.Media.Imaging
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows.Browser

Partial Public Class imageZoom
    Inherits UserControl

    Public pv As PictureView

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Setup(ByVal pv As PictureView)
        Me.pv = pv
        BoxContents.DataContext = pv
    End Sub


    Private Shared _instance As Dialog
    Private Shared ReadOnly Property dialogInstance() As Dialog
        Get
            If _instance Is Nothing Then _instance = New Dialog()
            Return _instance
        End Get
    End Property

    Public Shared Sub Show(ByVal photos As ObservableCollection(Of Photo), ByVal index As Integer)
        Dim iz As New imageZoom()
        iz.Setup(New PictureView(photos, index))
        Dim userCtl As UserControl = CType(Application.Current.RootVisual, UserControl)
        Dim layoutRoot As Panel = CType(userCtl.GetValue(ContentProperty), Panel)
        dialogInstance.Show(iz, layoutRoot)
    End Sub

    Public Shared Sub Hide()
        If _instance IsNot Nothing Then
            _instance.Close()
            _instance = Nothing
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Hide()
    End Sub

    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNext.Click
        pv.Index += 1
    End Sub

    Private Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrev.Click
        pv.Index -= 1
    End Sub

    Private Sub btnPicnik_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPicnik.Click
        System.Windows.Browser.HtmlPage.PopupWindow(New Uri(App.baseUri & "/OpenPicnik.aspx?imgUrl=" & Uri.EscapeDataString(pv.CurrentPhotoURL)), _
                                                    "_blank", New HtmlPopupWindowOptions() With {.Menubar = True, .Resizeable = True})

    End Sub

    Private Sub img_ImageOpened(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles img.ImageOpened
        If img.ActualHeight > Me.LayoutRoot.ActualHeight OrElse img.ActualWidth > Me.LayoutRoot.ActualWidth Then
            img.Height = Me.LayoutRoot.ActualHeight - 40
            img.Width = Me.LayoutRoot.ActualWidth - 40
            img.Stretch = Stretch.Uniform
        Else
            img.Height = Double.NaN
            img.Width = Double.NaN
            img.Stretch = Stretch.None
        End If
    End Sub
End Class

